package cate.game.fight.msg;


import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.common.table.fight.preview.FightPreviewRow;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBFight;
import cate.game.fight.test.TestFightCtx;
import cate.game.play.part.BattleHero;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

@ClientMsg(title = "战斗预览 の 请求")
public class FightPreviewReq implements MsgInput {


    @NoteField("英雄原型id")
    public int heroProtoId;

    @NoteField(value = "是否携带灵器",detail = "默认是带的")
    public boolean fairy = true;

    @NoteField(value = "皮肤",detail = "默认不穿皮肤")
    public int skin;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        Role role = cc.getRole();
        EcResult r = settle(role);
        role.getGame().notice.message(role, r, true);
    }


    private EcResult settle(Role role) {
        EcResult r = new EcResult();
        FightPreviewRow row = role.getGame().table.fightPreview.base.get(heroProtoId);
        if (row == null) {
            return r.fail("配置不存在");
        }
        FightSide a = role.getGame().fight.side.createFightSideByCode(GDFight.Team.A, row.insA).data;
        FightSide b = role.getGame().fight.side.createFightSideByCode(GDFight.Team.B, row.insB).data;
        if (a == null || b == null) {
            return r.fail("阵容配置不存在");
        }
        TestFightCtx ctx = new TestFightCtx();
        ctx.funcId = GDFunc.FIGHT_PREVIEW;
        for (BattleHero h : a.ps.hs) {
            if (h.hero.getTpl(role.getGame().fight.getPlayBuildContext()).protoId == heroProtoId) {
                if (fairy && row.fairySkill > 0) {
                    ctx.param.sideA.skillAppend.addHeroProtoSkill(heroProtoId, row.fairySkill);
                }
                if (skin > 0) {
                    h.skin = skin;
                }
                break;
            }
        }
        EcResult<PlayPO> f_r = role.getGame().play.startBySystem(ctx, a, b);
        if (f_r.ok()) {
            f_r.data.story.params.put("heroProtoId", heroProtoId);
            f_r.data.story.params.put("fairy", fairy);
            f_r.data.story.params.put("skin", skin);
            role.getCc().sendNow(new FightPlayResp(GDFunc.FIGHT_PREVIEW, f_r.data));
        } else {
            role.getGame().notice.message(role, f_r);
        }
        return r.success();
    }



    @Override
    public MsgConfig conf() {
        return MsgIdBFight.FIGHT_PREVIEW_REQ;
    }



}
